﻿// Copyright richyp 2009\n\nusing System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Richyp.Bodger.UI {
    public class NiceRenderer : DataGridViewTextBoxCell  {
        private string[] rows;
        private ListEntry entry = null;
        public NiceRenderer(string content,ListEntry entry) {            
            this.Value = content;
            this.SetContent(content);
            this.IsCategoryRow = false;
            this.entry = entry;
        }

        public NiceRenderer(string content) : this(content,null) {
        }

        public void SetContent(string content) {
            this.SetContent(content,true);
        }
        public void SetContent(string content,bool splitRows) {
            this.Value = content;
            this.splitRows = splitRows;
            rows = content.Split(new char[] { '\n' });
        }

        public bool IsCategoryRow { get; set; }
        private bool splitRows = true;

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {
            Color fromColor = SystemColors.ControlLightLight;
            Color toColor = SystemColors.Control;
            Color textColor = SystemColors.ControlText;
            if (this.Selected) {
                fromColor = SystemColors.ActiveCaption;
                toColor = SystemColors.GradientActiveCaption;
                textColor = SystemColors.ActiveCaptionText;
            }
            if (this.IsCategoryRow) {
                fromColor = Color.Yellow;
                toColor = Color.Orange;
                textColor = Color.Black;
            }
            LinearGradientBrush lgb = new LinearGradientBrush(cellBounds, fromColor, toColor,90);
            graphics.FillRectangle(lgb, cellBounds);
            if (!this.IsCategoryRow) {
                graphics.DrawRectangle(SystemPens.ControlDark, cellBounds);
            }
            float x = 2;
            float y = 2;
            int count = 0;
            foreach (string row in this.rows) {
                Font f = SystemFonts.MessageBoxFont;
                string text = row;
                if (count == 0) {
                    if (text.IndexOf('.') > -1 && this.IsCategoryRow) {
                        text = row.Substring(text.IndexOf('.')+1);
                    } else if (text.IndexOf('.') > -1 && !this.IsCategoryRow) {
                        text = "";
                    }
                }
                float stringHeight = graphics.MeasureString(text, f).Height;
                if (count == 0) {
                    f = SystemFonts.SmallCaptionFont;
                    stringHeight = graphics.MeasureString(row, f).Height;
                    //graphics.DrawLine(SystemPens.ControlDark, cellBounds.X + x, cellBounds.Y + stringHeight, cellBounds.Width - (x * 2), cellBounds.Y + stringHeight);
                }

                if (this.splitRows) {
                    Font temp = new Font(f,FontStyle.Regular);
                    string[] rows = text.Split(new char[] { ',' });
                    foreach (string t in rows) {
                        Color c = textColor;
                        text = t;
                        if (text.StartsWith("[")) {
                            temp = new Font(f, FontStyle.Bold);
                            text = text.Replace("[", "");
                            text = text.Replace("]", "");
                            if (text.IndexOf(".") > -1) {
                                text = text.Substring(text.IndexOf(".") + 1);
                            }
                            c = Color.FromArgb(90, 90, 90);
                        } else if (text.StartsWith("-")) {
                            temp = new Font(f, FontStyle.Bold);
                            text = text.Replace("-", "");
                        }
                        if (t.IndexOf(":") > -1) {
                            text = t.Substring(0,t.IndexOf(":"));
                            string pts = t.Substring(t.IndexOf(":")+1);
                            if (pts.IndexOf(",") > -1) {
                                pts = pts.Substring(0, pts.IndexOf(","));
                            }
                            graphics.DrawString(pts, new Font(temp, FontStyle.Bold), new SolidBrush(Color.DarkGray), cellBounds.X + cellBounds.Width - 45, cellBounds.Y + y);
                        }
                        if( text.StartsWith("#") ) {
                            text = text.Substring(1) + " pts";
                            temp = new Font(temp,FontStyle.Bold);
                        }
                        SizeF fs = graphics.MeasureString(text, temp);
                        Rectangle bounds = new Rectangle((int)(cellBounds.X + x), (int)(cellBounds.Y + y), (int)cellBounds.Width - 50, cellBounds.Height);
                        if (fs.Width > bounds.Width && text.Length > 0 && ! text.EndsWith( "pts" ) )  {
                            bool shrunk = false;
                            int l = text.Length - 5;                           
                            while (!shrunk && l > 0 ) {
                                string ntext = text.Substring(0, l) + "...";
                                SizeF newFs = graphics.MeasureString(ntext, temp);
                                if (newFs.Width <= bounds.Width) {
                                    text = ntext;
                                    shrunk = true;
                                }
                                l--;
                            }
                        }
                        graphics.DrawString(text, temp, new SolidBrush(c), bounds);
                        
                        y += stringHeight;
                    }
                    y -= stringHeight;
                } else {
                    Font temp = new Font(f,FontStyle.Regular);
                    if (text.IndexOf(":") > -1) {
                        text = text.Substring(0, text.IndexOf(":"));
                        string pts = row.Substring(row.IndexOf(":"));
                        if (pts.IndexOf(",") > -1) {
                            pts = pts.Substring(0, pts.IndexOf(","));
                        }
                        graphics.DrawString(pts, new Font(temp,FontStyle.Bold), new SolidBrush(Color.DarkGray), cellBounds.X + cellBounds.Width - 45, cellBounds.Y + y);
                    }
                    if (text.StartsWith("-")) {
                        text = text.Replace("-", "");
                        temp = new Font(f, FontStyle.Bold);
                    }
                    if(text.StartsWith("#" )) {
                        text = text.Substring(1);
                        temp = new Font(temp,FontStyle.Bold);
                    }
                    graphics.DrawString(text, temp, new SolidBrush(textColor), cellBounds.X + x, cellBounds.Y + y);
                }
                y += stringHeight;
                count++;
            }
           
            //base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }
    }
}
